package mybatis.objectFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.apache.bcel.generic.FieldGenOrMethodGen;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.omg.CORBA.FieldNameHelper;

public class ExampleObjectFactory<E> extends DefaultObjectFactory {
    private E entity = null;
    private String objectFlag;

    public Object create(Class type) {
        return super.create(type);
    }

    public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
        T entity = super.create(type, constructorArgTypes, constructorArgs);
        Field field;
        if (!(entity instanceof ArrayList<?>)) {
            try {
                field = entity.getClass().getDeclaredField("objectFlag");
                if (field != null) {
                    field.setAccessible(true);
                    Class<?> fieldType = field.getType();
                    field.set(entity, fieldType.cast(109));
                }
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return entity;
    }

    public void setProperties(Properties properties) {
        if (entity != null) {
            Class<?> clazz = entity.getClass();
            Enumeration<Object> enumeration = properties.keys();
            while (enumeration.hasMoreElements()) {
                String filedName = (String) enumeration.nextElement();
                try {
                    Field field = clazz.getField(filedName);
                    Class<?> fieldType = field.getType();
                    if (field != null) {
                        field.set(entity, fieldType.cast(properties.get(filedName)));
                    }
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }

    public <T> boolean isCollection(Class<T> type) {
        return Collection.class.isAssignableFrom(type);
    }
}
